package com.htj.erp.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.htj.erp.service.ProductService;
import com.htj.erp.vo.ProductVO;
import com.kedacom.utils.ArrayUtil;
import com.kedacom.utils.RequestUtil;
import com.kedacom.web.ResultMessage;
import com.kedacom.web.controller.BaseController;

/**
 * 
 * 
 * product的界面处理controller
 * ProductController
 * 继承com.kedacom.web.controller.BaseController 
 * chenghongtao
 * 2016-11-04 23:25:15
 * HTJ
 * 
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping({ "/platform/product" })
public class ProductController extends BaseController {

	@Resource
	private ProductService productService;
	
	 /**
     * 
     * page 列表页面
     * @return
     * @throws Exception
     *ModelAndView
     * @exception
     * @since  1.0.0
     */
	@RequestMapping("/page.do")
    public ModelAndView page() throws Exception {
        return getAutoView();
    }
    
    /**
	 * 
	 * listData 查询所有数据 通过界面查询
	 * @param pageable
	 * @return
	 * @throws Exception
	 *Page<ProductPO>
	 * @exception
	 * @since  1.0.0
	 */
    @RequestMapping("/listData.do")
    @ResponseBody
    public Page<ProductVO> listData(Pageable pageable) throws Exception
    {
    	Map<String,Object> map = RequestUtil.getParameterValueMap(request, false,false);
    	if(!map.containsKey("status"))
    		map.put("status", 1);
        return productService.listByPage(map, pageable);
    }
	 
	 
	 /**
	 * 查看界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/view.do")
	public ModelAndView view(Long id) throws Exception
	{
		if(id== null || id==0)
		{
			return this.getAutoView();
		}
		else
		{
			return this.getAutoView().addObject("vo", this.productService.getById(id));
		}
	}
	
	/**
	 * 编辑界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/editForm.do")
	public ModelAndView editForm(Long id) throws Exception
	{
		if(id== null || id==0)
		{
			return this.getAutoView();
		}
		else
		{
			return this.getAutoView().addObject("vo", this.productService.getById(id));
		}
	}
	
	/**
     * 
     * saveOrUpdate 保存数据
     * @param productPO
     * @return
     * @throws Exception
     *ResultMessage
     * @exception
     * @since  1.0.0
     */
    @RequestMapping("/saveOrUpdate.do")
    @ResponseBody
    public ResultMessage saveOrUpdate(ProductVO productVO) throws Exception
    {
        productVO = this.productService.saveOrUpdate(productVO);
        return new ResultMessage(ResultMessage.Success,"保存成功",productVO);
        
    }
    
    
    
     /**
     * 
     * delById 根据ID删除
     * @param id
     * @return
     * @throws Exception
     *ResultMessage
     * @exception
     * @since  1.0.0
     */
    @RequestMapping("/delById.do")
    @ResponseBody
    public ResultMessage delById(Long id) throws Exception
    {
        this.productService.delById(id);
        return new ResultMessage(ResultMessage.Success,"删除成功");
    }

    /**
     * 
     * delByIds 根据多个ID字符串删除
     * @param ids
     * @return
     * @throws Exception
     *ResultMessage
     * @exception
     * @since  1.0.0
     */
     @RequestMapping("/delByIds.do")
    @ResponseBody
    public ResultMessage delByIds(String ids) throws Exception
    {
        this.productService.delByIds(ArrayUtil.convertArray(ids.split(",")));
        return new ResultMessage(ResultMessage.Success,"删除成功");
    }
     
     @RequestMapping("/selector.do")
     public ModelAndView selector() throws Exception {
         return getAutoView();
     } 
     
     @RequestMapping("/listAllData.do")
     @ResponseBody
     public List<ProductVO> listAllData() throws Exception
     {
         return productService.getAll();
     }
     
     
     @RequestMapping("/stock.do")
     public ModelAndView stock() throws Exception {
         return getAutoView();
     } 
     
     
     @RequestMapping("/listProductSumInfo.do")
     @ResponseBody
     public Map listProductSumInfo() throws Exception
     {
         return productService.listProductSumInfo();
     }
}
